﻿Imports Microsoft.Reporting.WinForms
Imports Lourdes.BackEnd.BC

Public Class frmReportViewer
  Private _ReportPath As String
  Private _Reporte As Lourdes.BackEnd.Enums.ReporteTipos
  Private _Parametros As ReportParameterCollection
  Private Const ReportDSName As String = "DSReports"

  Private Sub frmReportViewer_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    'Dim oReport As Report
    With Me.ReportViewer1
      .LocalReport.DataSources.Add(GetReportDataSource)

      .LocalReport.ReportPath = _ReportPath

      .RefreshReport()
    End With
  End Sub

  Private Function GetReportDataSource() As ReportDataSource
    GetReportDataSource = Nothing
    Try
      Select Case _Reporte
        Case BackEnd.Enums.ReporteTipos.Factura
          Return GetReportDataSourceFactura()
        Case BackEnd.Enums.ReporteTipos.Recibo
          Return GetReportDataSourceRecibo()
        Case BackEnd.Enums.ReporteTipos.Remito
          Return GetReportDataSourceRemito()
      End Select
    Catch ex As Exception
      Throw ex
    End Try
  End Function

  Private Function GetReportDataSourceFactura() As ReportDataSource
    Dim FacturaId As Integer = _Parametros.Item("FacturaId").Values(0)
    Return (New ReportDataSource(ReportDSName, (New ReportSources).getFactura(FacturaId)))
  End Function

  Private Function GetReportDataSourceRecibo() As ReportDataSource
    Dim DocReciboId As Integer = _Parametros.Item("DocReciboId").Values(0)
    Return (New ReportDataSource(ReportDSName, (New ReportSources).getRecibo(DocReciboId)))
  End Function

  Private Function GetReportDataSourceRemito() As ReportDataSource
    Dim RemitoId As Integer = _Parametros.Item("DocRemitoId").Values(0)
    Return (New ReportDataSource(ReportDSName, (New ReportSources).getRemito(RemitoId)))
  End Function

  Private Sub ServerReport(p1 As Object)
    Throw New NotImplementedException
  End Sub

  Public Sub New(pReporteTipo As Lourdes.BackEnd.Enums.ReporteTipos, pParams As ReportParameterCollection)
    ' Llamada necesaria para el diseñador.

    InitializeComponent()
    ' Agregue cualquier inicialización después de la llamada a InitializeComponent().
    _Reporte = pReporteTipo
    _Parametros = pParams

    Select Case _Reporte
      Case Lourdes.BackEnd.Enums.ReporteTipos.Factura
        _ReportPath = "..\..\Reports\Factura.rdlc"
        With Me
          .Width = 680
          .Height = 620
        End With        
      Case Lourdes.BackEnd.Enums.ReporteTipos.Recibo
        _ReportPath = "..\..\Reports\Recibo.rdlc"
        With Me
          .Width = 680
          .Height = 440
        End With
      Case Lourdes.BackEnd.Enums.ReporteTipos.Remito
        _ReportPath = "..\..\Reports\Remito.rdlc"
        With Me
          .Width = 680
          .Height = 430
        End With
    End Select
  End Sub
End Class